package cn.cjj.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import cn.cjj.common.exception.BizCodeEnume;
import cn.cjj.common.vo.MemberResponseVo;
import cn.cjj.gulimall.auth.vo.UserLoginVo;
import cn.cjj.gulimall.member.exception.EmailExistException;
import cn.cjj.gulimall.member.exception.PhoneExistException;
import cn.cjj.gulimall.member.exception.UsernameExistException;
import cn.cjj.gulimall.member.feign.ProductFeignService;
import cn.cjj.gulimall.member.vo.SocialUser;
import cn.cjj.gulimall.member.vo.UserRegisterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.cjj.gulimall.member.entity.MemberEntity;
import cn.cjj.gulimall.member.service.MemberService;
import cn.cjj.common.utils.PageUtils;
import cn.cjj.common.utils.R;



/**
 * 会员
 *
 * @author cjj
 * @email cjj@gmail.com
 * @date 2021-05-28 10:50:29
 */
@RefreshScope
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    ProductFeignService productFeignService;


    @RequestMapping("/oauth2/login")
    public R oauthLogin(@RequestBody SocialUser user) {
        MemberEntity member = memberService.login(user);

        if (member == null) {
            return R.error(BizCodeEnume.USER_LOG_EXCEPTION.getCode(), BizCodeEnume.USER_LOG_EXCEPTION.getMsg());
        }

        MemberResponseVo memberResponse = new MemberResponseVo();
        BeanUtils.copyProperties(member, memberResponse);

        return R.ok().put("data", memberResponse);
    }

    @RequestMapping("/login")
    public R login(@RequestBody UserLoginVo user) {

        MemberEntity member = memberService.login(user);

        if (member == null) {
            return R.error(BizCodeEnume.USER_LOG_EXCEPTION.getCode(), BizCodeEnume.USER_LOG_EXCEPTION.getMsg());
        }

        MemberResponseVo memberResponse = new MemberResponseVo();
        BeanUtils.copyProperties(member, memberResponse);

        return R.ok().put("data", memberResponse);
    }


    @RequestMapping("/register")
    public R register(@RequestBody UserRegisterVo user) {
        try {
            memberService.register(user);
        } catch (UsernameExistException usernameExistException) {
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(), BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        } catch (PhoneExistException phoneExistException) {
            return R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (EmailExistException emailExistException) {
            return R.error(BizCodeEnume.EMAIL_EXIST_EXCEPTION.getCode(), BizCodeEnume.EMAIL_EXIST_EXCEPTION.getMsg());
        } catch (Exception e) {
            return R.error();
        }

        return R.ok();
    }


    @RequestMapping("/testFeign")
    public R testFeign() {
        R r = productFeignService.listTree();
        return r;
    }

    @Value("${module.name}")
    private String moduleName;
    @RequestMapping("/module/name")
    public R getModuleName() {
        return R.ok().put("moduleName", moduleName);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
